//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "GaussianKernel.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/KernelFunction.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (Double average, Double sigma) : CKernelFunction(CGaussianKernel::typeid)
{
    try
    {
#ifdef HANDLE
        _phGaussianKernel = NULL;
#endif
        QuantLib::Real _average = (QuantLib::Real)ValueHelper::Convert (average);
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma);
        _ppGaussianKernel = new boost::shared_ptr<QuantLib::GaussianKernel> (new QuantLib::GaussianKernel ( _average,  _sigma ));
        SetKernelFunction (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (boost::shared_ptr<QuantLib::GaussianKernel>& childNative, Object^ owner) : CKernelFunction(CGaussianKernel::typeid)
{
#ifdef HANDLE
	_phGaussianKernel = NULL;
#endif
	_ppGaussianKernel = &childNative;
    _ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
}
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (QuantLib::GaussianKernel& childNative, Object^ owner) : CKernelFunction(CGaussianKernel::typeid)
{
#ifdef HANDLE
	_phGaussianKernel = NULL;
#endif
	_ppGaussianKernel = new boost::shared_ptr<QuantLib::GaussianKernel> (&childNative);
    _ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
    _GaussianKernelOwner = owner;
    _KernelFunctionOwner = owner;
}

Cephei::QL::Math::CGaussianKernel::CGaussianKernel (CGaussianKernel^ copy) : CKernelFunction(CGaussianKernel::typeid)
{
#ifdef HANDLE
	_phGaussianKernel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGaussianKernel = new boost::shared_ptr<QuantLib::GaussianKernel> (copy->GetShared());
        _ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
    }
}
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (System::Type^ t) : CKernelFunction(CGaussianKernel::typeid)
{
#ifdef HANDLE
	_phGaussianKernel = NULL;
#endif
	if (!t->IsSubclassOf(CGaussianKernel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (QuantLib::Handle<QuantLib::GaussianKernel>& childNative, Object^ owner)  : CKernelFunction(CGaussianKernel::typeid)
{
	_phGaussianKernel = &childNative;
	_ppGaussianKernel = &static_cast<boost::shared_ptr<QuantLib::GaussianKernel>>(childNative.currentLink());
    _ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
    _GaussianKernelOwner = owner;
}
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (QuantLib::Handle<QuantLib::GaussianKernel> childNative)  : CKernelFunction(CGaussianKernel::typeid)
{
	_phGaussianKernel = &childNative;
	_ppGaussianKernel = &static_cast<boost::shared_ptr<QuantLib::GaussianKernel>>(childNative.currentLink());
    _ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CGaussianKernel::CGaussianKernel (QuantLib::GaussianKernel childNative)  : CKernelFunction(CGaussianKernel::typeid)
{
#ifdef HANDLE
	_phGaussianKernel = NULL;
#endif
	_ppGaussianKernel = new boost::shared_ptr<QuantLib::GaussianKernel> (new QuantLib::GaussianKernel (childNative));
    _ppKernelFunction = new boost::shared_ptr<QuantLib::KernelFunction> (boost::dynamic_pointer_cast<QuantLib::KernelFunction> (*_ppGaussianKernel));
}
#endif

Cephei::QL::Math::CGaussianKernel::~CGaussianKernel ()
{
    if (_ppGaussianKernel != NULL)
    {
	    delete _ppGaussianKernel;
        _ppGaussianKernel = NULL;
    }
}
Cephei::QL::Math::CGaussianKernel::!CGaussianKernel ()
{
    if (_ppGaussianKernel != NULL)
    {
	    delete _ppGaussianKernel;
    }
}
QuantLib::GaussianKernel& Cephei::QL::Math::CGaussianKernel::GetReference ()
{
    if (_ppGaussianKernel == NULL) throw gcnew NativeNullException ();
	return **_ppGaussianKernel;
}
boost::shared_ptr<QuantLib::GaussianKernel>& Cephei::QL::Math::CGaussianKernel::GetShared ()
{
    if (_ppGaussianKernel == NULL) throw gcnew NativeNullException ();
	return *_ppGaussianKernel;
}
QuantLib::GaussianKernel* Cephei::QL::Math::CGaussianKernel::GetPointer ()
{
    if (_ppGaussianKernel == NULL) throw gcnew NativeNullException ();
	return &**_ppGaussianKernel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GaussianKernel>& Cephei::QL::Math::CGaussianKernel::GetHandle ()
{
	if (_phGaussianKernel == NULL)
	{
		_phGaussianKernel = new Handle<QuantLib::GaussianKernel> (*_ppGaussianKernel);
	}
	return *_phGaussianKernel;
}
#endif
bool Cephei::QL::Math::CGaussianKernel::HasNative () 
{
	return (_ppGaussianKernel != NULL);
}

Double Cephei::QL::Math::CGaussianKernel::Derivative (Double x)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGaussianKernel)->derivative ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CGaussianKernel::Primitive (Double x)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGaussianKernel)->primitive ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::IGaussianKernel^ Cephei::QL::Math::CGaussianKernel_Factory::Create (Double average, Double sigma)
{
    return gcnew CGaussianKernel ( average,  sigma);
}
